package com.example.book_recommend.service.impl;

import com.example.book_recommend.dao.BookInfoMapper;
import com.example.book_recommend.entity.BookInfo;
import com.example.book_recommend.service.BookService;
import org.assertj.core.groups.Tuple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service("BookService")
public class BookServiceImpl implements BookService {
    @Autowired
    private BookInfoMapper bookInfoMapper;

    @Override
    public BookInfo getBookInfo(Long id) {
        BookInfo thisBook = bookInfoMapper.selectByPrimaryKey(id);
        thisBook.setLookMount(thisBook.getLookMount() + 1);
        bookInfoMapper.updateByPrimaryKey(thisBook);
        return thisBook;
    }

    @Override
    public BookInfo getBookByName(String name) {
        BookInfo thisBook = bookInfoMapper.selectByName(name);
        thisBook.setLookMount(thisBook.getLookMount() + 1);
        bookInfoMapper.updateByPrimaryKey(thisBook);
        return thisBook;
    }

    @Override
    public List<BookInfo> getBookByPage(Integer page, Integer size, String name) {
        return bookInfoMapper.getBookByPage(page, size, name);
    }

    @Override
    public List<BookInfo> getBookByPage(Integer page, Integer size) {
        return bookInfoMapper.getBookByPage0(page, size);
    }

    @Override
    public List<BookInfo> getBookByPage(Integer page, Integer size, Integer storeId) {
        return bookInfoMapper.getBookByPage1(page, size, storeId);
    }

    @Override
    public List<BookInfo> getBookByPage(Integer page, Integer size, Integer storeId, Integer isShelf) {
        return bookInfoMapper.getBookByPage2(page, size, storeId, isShelf);
    }

    @Override
    public int insert(BookInfo bookInfo) {
        return bookInfoMapper.insert(bookInfo);
    }

    @Override
    public int update(BookInfo bookInfo) {
        return bookInfoMapper.updateByPrimaryKeySelective(bookInfo);
    }

    @Override
    public int setIsOnShelf(Integer bookId, Integer onShelf) {
        BookInfo bookInfo = new BookInfo();
        bookInfo.setBookId(bookId.longValue());
        bookInfo.setIsShelf(onShelf);
        return bookInfoMapper.updateByPrimaryKeySelective(bookInfo);
    }

    @Override
    public int getBookPageSizeByPage(Integer storeId) {
        return bookInfoMapper.getPageSizeByStoreId(storeId);
    }

    @Override
    public ArrayList<ArrayList<BookInfo>> getBookByCate(ArrayList<Integer> cartIds,Integer page,Integer size) {
        ArrayList<ArrayList<BookInfo>> bookInfos = new ArrayList<>();
        for (Integer id : cartIds){
            bookInfos.add(bookInfoMapper.selectByCategory(id, page, size));
        }
        return bookInfos;
    }

    @Override
    public ArrayList<BookInfo> getTop50All() {
        return bookInfoMapper.selectTop50();
    }
}
